package com.esunny.resourcevisualization.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esunny.resourcevisualization.domain.entity.ResourceType;
import com.esunny.resourcevisualization.domain.entity.UserGroup;
import com.esunny.resourcevisualization.manage.ResourceTypeManage;
import com.esunny.resourcevisualization.manage.SystemConfigManage;
import com.esunny.resourcevisualization.manage.UserGroupManage;
import com.esunny.resourcevisualization.service.RouterService;
import com.esunny.resourcevisualization.util.HostHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0.0.0
 * @ClassName RouterServiceImpl
 * @Description 路由管理服务接口实现
 * @Author baochencong
 * @Date 2020/10/11 19:15
 */
@Service
public class RouterServiceImpl implements RouterService {

    @Resource
    private HostHolder hostHolder;

    @Resource
    private UserGroupManage userGroupManage;

    @Resource
    private ResourceTypeManage resourceTypeManage;

    @Resource
    private SystemConfigManage systemConfigManage;

    @Override
    public JSONArray getAllRouter() {
        /*String allRouter = "[{'path':'/','component':'Layout','redirect':'index','children':[{'path':'index','name':'Index','component':'@/views/index/index','meta':{'title':'首页','icon':'home','affix':true}}]},"+
                "{'path':'/myLocation','name':'MyLocation','component':'Layout','redirect':'noRedirect','alwaysShow':true,'meta':{'title':'我的地盘','icon':'user'},'children':[{'path':'myResource','name':'MyResource','component':'@/views/test/helloworld','meta':{'title':'我的资源'}},{'path':'myMessage','name':'MyMessage','component':'@/views/test/helloworld','meta':{'title':'我的消息'}}]}," +
                "{'path':'/resourceManage','name':'ResourceManage','component':'Layout','redirect':'noRedirect','alwaysShow':true,'meta':{'title':'资源管理','icon':'user'},'children':[]}," +
                "{'path':'/resourceType','name':'ResourceType','component':'Layout','redirect':'noRedirect','alwaysShow':true,'meta':{'title':'资源类别','icon':'user'},'children':[{'path':'resourceTypeManage','name':'ResourceTypeManage','component':'@/views/test/helloworld','meta':{'title':'类别管理'}},{'path':'enumData','name':'EnumData','component':'@/views/test/helloworld','meta':{'title':'资源数据字典'}}]},"+
                "{'path':'/systemManage','name':'SystemManage','component':'Layout','redirect':'noRedirect','alwaysShow':true,'meta':{'title':'系统管理','icon':'user'},'children':[{'path':'operateLog','name':'OperateLog','component':'@/views/test/helloworld','meta':{'title':'操作日志'}}]}]";
        */
        String baseRouterInfo = systemConfigManage.getConfigByKey("routerInfo").getValue();
        JSONArray allRouterInfo = addResourceTypeRouterToBaseRouterInfo(baseRouterInfo);
        addUserGroupRouterToAllRouterInfo(allRouterInfo);
        return getUserRouterInfo(allRouterInfo);
    }

    /**
     * 添加资源类别路由信息到json格式全部路由信息
     *
     * @param baseRouterInfo 基本路由信息
     * @return 全部路由信息
     */
    private JSONArray addResourceTypeRouterToBaseRouterInfo(String baseRouterInfo) {
        JSONArray allRouterInfo = JSONArray.parseArray(baseRouterInfo);
        List<ResourceType> resourceTypeList = resourceTypeManage.getResourceTypeList();
        String resourceTypeRouterInfo;
        JSONObject resourceTypeMenuRouterInfo = getMenuRouterInfoByMenuName(allRouterInfo, "/resourceManage");
        for (ResourceType resourceTypeItem : resourceTypeList) {
            String resourceTypeName = resourceTypeItem.getResourceTypeName();
            String resourceTypeTitle = resourceTypeItem.getResourceTypeTitle();
            String childrenRouterName = resourceTypeName.substring(0, 1).toUpperCase() + resourceTypeName.substring(1);
            resourceTypeRouterInfo = "{'path':'" + resourceTypeName + "','name':'+" + childrenRouterName + "+','component':'@/views/pages/resourceentity/index','meta':{'title':'" + resourceTypeTitle + "'}}";
            assert resourceTypeMenuRouterInfo != null;
            resourceTypeMenuRouterInfo.getJSONArray("children").add(JSONObject.parseObject(resourceTypeRouterInfo));
        }
        return allRouterInfo;
    }

    /**
     * 添加用户组路由信息到json格式全部路由信息
     *
     * @param allRouterInfo 全部路由信息
     */
    private void addUserGroupRouterToAllRouterInfo(JSONArray allRouterInfo) {
        List<UserGroup> userGroupList = userGroupManage.getUserGroupList();
        String resourceTypeRouterInfo;
        JSONObject resourceTypeMenuRouterInfo = getMenuRouterInfoByMenuName(allRouterInfo, "/groupResource");
        for (UserGroup userGroup : userGroupList) {
            int id = userGroup.getId();
            String userGroupName = userGroup.getUserGroupName();
            String childrenRouterName = "Index" + id;
            resourceTypeRouterInfo = "{'path':'" + id + "','name':'+" + childrenRouterName + "+','component':'@/views/pages/groupresource/index','meta':{'title':'" + userGroupName + "'}}";
            assert resourceTypeMenuRouterInfo != null;
            resourceTypeMenuRouterInfo.getJSONArray("children").add(JSONObject.parseObject(resourceTypeRouterInfo));
        }
    }

    /**
     * 获取当前用户可访问路由
     *
     * @param allRouterInfo 全部路由信息
     * @return 当前用户可访问路由信息
     */
    private JSONArray getUserRouterInfo(JSONArray allRouterInfo) {
        if (hostHolder.getUser().getUserLevel() != 1) {
            allRouterInfo.remove(getMenuRouterInfoByMenuName(allRouterInfo, "/systemManage"));
            allRouterInfo.remove(getMenuRouterInfoByMenuName(allRouterInfo, "/resourceType"));
            allRouterInfo.remove(getMenuRouterInfoByMenuName(allRouterInfo, "/groupResource"));
            JSONObject myLocation = getMenuRouterInfoByMenuName(allRouterInfo, "/myLocation");
            JSONArray myLocationChildren = myLocation.getJSONArray("children");
            myLocationChildren.remove(getMenuRouterInfoByMenuName(myLocationChildren,"auditApply"));
        }
        return allRouterInfo;
    }

    /**
     * 获取路由一级菜单信息
     *
     * @param allRouterInfo 所有一级菜单信息
     * @param menuName      一级菜单名
     * @return 该一级菜单JSON
     */
    private JSONObject getMenuRouterInfoByMenuName(JSONArray allRouterInfo, String menuName) {
        for (Object menu : allRouterInfo) {
            JSONObject menuJSON = (JSONObject) menu;
            if (menuJSON.getString("path").equals(menuName)) {
                return menuJSON;
            }
        }
        return null;
    }
}
